<template>
  <div>
    <ConTitle :topTitle="topTitle"/>
    <el-card class="box-card middle">
      <div class="clearfix">
        <span>群组名称</span>
        <span class="seach">
          <el-input v-model="query.keyword" placeholder="群组名称"></el-input>
        </span>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <span class="result">搜索结果：{{ total }}</span>
        <el-button type="primary" class="group" @click="addDialog">新增群组</el-button>
      </div>
    </el-card>
    <!--  显示表格数据-->
    <el-table :data="deviceGroupList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="groupName" label="群组名称"></el-table-column>
      <el-table-column prop="nodeNums" label="传感器节点数量"></el-table-column>
      <el-table-column prop="onlineNums" label="在线设备数量"></el-table-column>
      <el-table-column prop="cTime" label="创建时间"></el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="showDialog(scope.row.id,scope.row.groupName)" type="text" size="small">重命名</el-button>
          <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currPage"
        :page-sizes="[5, 7, 10]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

<!--    弹窗 新增或修改-->

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm" label-width="100px">
        <el-form-item label="群组名称" prop="groupName">
          <el-input v-model="ruleForm.groupName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleOk">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
import {$deviceGroupAdd, $deviceGroupDel, $deviceGroupDit, $deviceGroupList} from "@/api";
import ConTitle from "@/components/ConTitle";
export default {
  name: "deviceGroup",
  components: {
    ConTitle,
  },
  data(){
    return {
      query:{//查询和分页传给后台的默认数据
        currPage:1,
        pageSize:5,
        keyword:"", //群组名称
      },
      total:0,
      deviceGroupList:[],
      topTitle: {
        type: "设备",
        text: "节点群组",
        msg: "以分组的形式管理节点设备，提供了搜索、添加新的群组、重命名、移动设备等功能。"
      },
      dialogVisible:false,
      ruleForm: { groupName:"" },
      rules: {
        //做验证处理 validate
        groupName: [
          {required:true,message:"请输入群组名称",trigger:"blue"},
        ],
      },
      editId: "",//要修改的数据id
    }
  },
  methods:{
    //删除
    handleDel(id){
      //删除前要确认
      this.$confirm("此操作将永久删除文件，是否继续？","提示",{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(()=>{
        $deviceGroupDel({id:id}).then(res=>{
          if (res.data.code == 666) {
            this.$message({
              type:"success",
              message:"删除成功！",
            });
            this.query.currPage = 1;
            this.devicegroupList();
          }
        });
      }).catch(()=>{
        this.$message({
          type:"info",
          message:"已取消删除",
        });
      });
    },
    //显示添加窗口
    addDialog(){
      this.dialogVisible = true;
      this.editId = "";
      this.ruleForm.groupName = "";
    },
    //显示修改窗口， 并 把群组信息带过去
    showDialog(id, groupName) {
      this.dialogVisible = true;
      this.editId = id;
      this.ruleForm.groupName = groupName;
    },
    //点击确认按钮
    handleOk(){
      //验证 表单提交的数据是否符合验证条件
      this.$refs.ruleForm.validate((vaild)=>{
        if (vaild) {
          //判断是添加还是修改
          if (this.editId !== ""){
            //修改
            this.editItem();
          }else {
            //添加
            this.addItem();
          }
        }else {
          return false;
        }
      });
    },
    //添加
    addItem(){
      //添加接口
      $deviceGroupAdd({ groupName:this.ruleForm.groupName }).then(res=>{
        console.log(res);
        //判断是否添加成功
        if (res.data.msg == "success") {
          //关闭窗口
          this.dialogVisible = false;
          //重新调用列表接口
          this.devicegroupList();
        }
      });
    },
    //修改
    editItem(){
      $deviceGroupDit({ id:this.editId, groupName:this.ruleForm.groupName }).then(res=>{
        //修改成功
        if (res.data.msg == "success"){
          //关闭窗口
          this.dialogVisible = false;
          //重新调用列表接口
          this.devicegroupList();
        }
      });
    },
    //搜索
    onSubmit(){
      this.devicegroupList();
    },
    devicegroupList(){
      $deviceGroupList(this.query).then(res=>{
        console.log(res.data);
        this.deviceGroupList = res.data.data;
        //给总数量赋值
        this.total = res.data.total;
      });
    },
    //修改分页的每页显示条数
    handleSizeChange(num){
      console.log("显示条数",num);
      this.query.pageSize = num;
      this.query.currPage = 1;
      this.devicegroupList();
    },
    //修改当前页页码
    handleCurrentChange(num){
      console.log("当前页",num);
      //将当前页的页码赋值给 currPage
      this.query.currPage = num;
      this.devicegroupList();
    }
  },
  mounted() {
    this.devicegroupList();
  }
}
</script>

<style scoped lang="scss">
.middle{
  margin-bottom: 10px;
  .seach{
    display: inline-block;
    width: 120px;
    margin:0 15px;
  }
  .result{
    font-size: 14px;
    margin-left: 50px;
  }
  .group{
    float: right;
  }
}
</style>